const IMAGE_REGEXP = /\.(jpeg|jpg|gif|png|svg|webp|jfif|bmp|dpg)/i;
const VIDEO_REGEXP = /\.(mp4|mpg|mpeg|dat|asf|avi|rm|rmvb|mov|wmv|flv|mkv)/i;

export function isImageUrl(url) {
  return IMAGE_REGEXP.test(url);
}

export function isVideoUrl(url) {
  return VIDEO_REGEXP.test(url);
}

export function isImageFile(item) {
  if (item.isImage) {
    return item.isImage;
  }

  if (item.type) {
    return item.type.indexOf('image') > -1;
  }

  if (item.url) {
    return isImageUrl(item.url);
  }

  return false;
}

export function isVideoFile(item) {
  if (item.isVideo) {
    return item.isVideo;
  }

  if (item.type) {
    return item.type.indexOf('video') > -1;
  }

  if (item.url) {
    return isVideoUrl(item.url);
  }

  return false;
}

function pickExclude(obj, keys) {
  // 某些情况下，type可能会为
  if (!['[object Object]', '[object File]'].includes(Object.prototype.toString.call(obj))) {
    return {};
  }
  return Object.keys(obj).reduce((prev, key) => {
    if (!keys.includes(key)) {
      prev[key] = obj[key];
    }
    return prev;
  }, {});
}

function formatImage(res) {
  return res.tempFiles.map(item => ({
    ...pickExclude(item, ['path']),
    type: 'image',
    url: item.path,
    thumb: item.path,
    size: item.size,
    // #ifdef H5
    name: item.name,
    // #endif
  }));
}

function formatVideo(res) {
  return [
    {
      ...pickExclude(res, ['tempFilePath', 'thumbTempFilePath', 'errMsg']),
      type: 'video',
      url: res.tempFilePath,
      thumb: res.thumbTempFilePath,
      size: res.size,
      // #ifdef H5
      name: res.name,
      // #endif
    },
  ];
}

function formatMedia(res) {
  return res.tempFiles.map(item => ({
    ...pickExclude(item, ['fileType', 'thumbTempFilePath', 'tempFilePath']),
    type: res.type,
    url: item.tempFilePath,
    thumb: res.type === 'video' ? item.thumbTempFilePath : item.tempFilePath,
    size: item.size,
  }));
}

function formatFile(res) {
  return res.tempFiles.map(item => ({
    ...pickExclude(item, ['path']),
    url: item.path,
    size: item.size,
    // #ifdef H5
    name: item.name,
    type: item.type,
    // #endif
  }));
}

// 格式化文件大小
export function formatSize(num = 0) {
  let str = num + 'B';
  if (typeof num === 'number') {
    [
      { value: 1024 * 1024, label: 'M' },
      { value: 1024, label: 'K' },
    ].some(({ value, label }) => {
      if (num >= value) {
        str = (num / value).toFixed(0) + label;
        return true;
      }
    });
  }
  return str;
}

// 选择文件
export function chooseFile({
  accept,
  multiple,
  sourceType,
  mediaType,
  sizeType,
  compressed,
  maxDuration,
  camera,
  maxCount,
}) {
  return new Promise((resolve, reject) => {
    switch (accept) {
      case 'image':
        uni.chooseImage({
          count: multiple ? Math.min(maxCount, 9) : 1,
          sourceType,
          sizeType,
          success: res => resolve(formatImage(res)),
          fail: reject,
        });
        break;
      case 'video':
        uni.chooseVideo({
          sourceType,
          compressed,
          maxDuration,
          camera,
          success: res => resolve(formatVideo(res)),
          fail: reject,
        });
        break;
      // #ifdef MP-WEIXIN
      // 只有微信小程序才支持chooseMedia接口
      case 'media':
        wx.chooseMedia({
          count: multiple ? Math.min(maxCount, 9) : 1,
          sourceType,
          mediaType,
          maxDuration,
          sizeType,
          camera,
          success: res => resolve(formatMedia(res)),
          fail: reject,
        });
        break;
      // #endif
      // #ifdef MP-WEIXIN || H5
      // 只有微信小程序才支持chooseMessageFile接口
      case 'file':
        // #ifdef MP-WEIXIN
        wx.chooseMessageFile({
          count: multiple ? maxCount : 1,
          type: accept,
          success: res => resolve(formatFile(res)),
          fail: reject,
        });
        // #endif
        // #ifdef H5
        // 需要hx2.9.9以上才支持uni.chooseFile
        uni.chooseFile({
          count: multiple ? maxCount : 1,
          type: accept,
          success: res => resolve(formatFile(res)),
          fail: reject,
        });
        // #endif
        break;
      // #endif
      default:
        // 此为保底选项，在accept不为上面任意一项的时候选取全部文件
        // #ifdef MP-WEIXIN
        wx.chooseMessageFile({
          count: multiple ? maxCount : 1,
          type: 'all',
          success: res => resolve(formatFile(res)),
          fail: reject,
        });
        // #endif
        // #ifdef H5
        // 需要hx2.9.9以上才支持uni.chooseFile
        uni.chooseFile({
          count: multiple ? maxCount : 1,
          type: 'all',
          success: res => resolve(formatFile(res)),
          fail: reject,
        });
      // #endif
    }
  });
}
